﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hi_Baidu.Packet.InPacket
{
    /// <summary>
    ///  * 获取指定分组下好友信息反馈包
    ///  * 基础包结构 
    ///  * 用户名 : String
    ///  * 用户好友帐号集合 : String[]
    ///  * 用户好友Id集合 : String[]
    ///  * 用户当前分组Id : String[]
    /// </summary>
    class GetFriendByGroupIdReplyPacket : BaseInPackage
    {
        public GetFriendByGroupIdReplyPacket(InPackage inpackage) : base(inpackage)
        {
            
        }

        public override void InitPackage()
        {
            int Offset = 0;
            //用户名长度
            byte[] UsernameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, UsernameLength, 0, 4);
            Offset += 4;
            //用户名长度
            byte[] Username = new byte[BitConverter.ToInt32(UsernameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Username, 0, Username.Length);
            Offset += Username.Length;
            //分组ID
            byte[] ItemId = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, ItemId, 0, 4);
            Offset += 4;
            //是否拥有好友的标志位
            byte[] HasFriend = new byte[1];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, HasFriend, 0, 1);
            Offset += 1;
            this.DIC_CONTENT.Add("USERNAME", Username);
            this.DIC_CONTENT.Add("ITEMID", ItemId);
            this.DIC_CONTENT.Add("HASFRIEND", HasFriend);
            //拥有好友
            if (BitConverter.ToBoolean(HasFriend, 0))
            {
                //好友ID长度
                byte[] FriendsIdLength = new byte[4];
                Array.ConstrainedCopy(this.Body["HEAD"], Offset, FriendsIdLength, 0, 4);
                Offset += 4;
                //好友ID
                byte[] FriendsId = new byte[BitConverter.ToInt32(FriendsIdLength, 0)];
                Array.ConstrainedCopy(this.Body["HEAD"], Offset, FriendsId, 0, FriendsId.Length);
                Offset += FriendsId.Length;
                //好友长度
                byte[] FriendsLength = new byte[4];
                Array.ConstrainedCopy(this.Body["HEAD"], Offset, FriendsLength, 0, 4);
                Offset += 4;
                //好友
                byte[] Friends = new byte[BitConverter.ToInt32(FriendsLength, 0)];
                Array.ConstrainedCopy(this.Body["HEAD"], Offset, Friends, 0, Friends.Length);
                Offset += Friends.Length;

                this.DIC_CONTENT.Add("FRIENDSID", FriendsId);
                this.DIC_CONTENT.Add("FRIENDS", Friends);
            }
        }

        /// <summary>
        ///     获取当前分组下所有好友列表
        /// </summary>
        /// <returns>
        ///     返回null , 则表示当前分组下没有好友
        /// </returns>
        public String[] GetFriends()
        {
            if (BitConverter.ToBoolean(this.DIC_CONTENT["HASFRIEND"], 0))
            {
                return Encoding.Default.GetString(this.DIC_CONTENT["FRIENDS"]).Split(new String[] { "," },
                                                                              StringSplitOptions.RemoveEmptyEntries);
            }
            return null;
        }

        /// <summary>
        ///     是否拥有好友
        /// </summary>
        /// <returns>
        ///     返回false,标示没有好友
        /// </returns>
        public bool HasFriend()
        {
            return BitConverter.ToBoolean(this.DIC_CONTENT["HASFRIEND"], 0);
        }

        /// <summary>
        ///     获取当前分组下所有好友ID列表
        /// </summary>
        /// <returns>
        ///     返回null , 则表示当前分组下没有好友
        /// </returns>
        public String[] GetFriendsId()
        {
            if (BitConverter.ToBoolean(this.DIC_CONTENT["HASFRIEND"], 0))
            {
                return Encoding.Default.GetString(this.DIC_CONTENT["FRIENDSID"]).Split(new String[] { "," },
                                                                                             StringSplitOptions.RemoveEmptyEntries);
            }
            return null;
        }

        /// <summary>
        ///     返回当前分组ID
        /// </summary>
        /// <returns>
        ///     A string value...
        /// </returns>
        public String GetItemId()
        {
            return BitConverter.ToInt32(this.DIC_CONTENT["ITEMID"], 0).ToString();
        }

    }
}
